<template>
  <div>
    <div class="container loginIn">
      <div class="left center">
        <el-form class="login-form" label-position="left" :label-width="'30px'">
          <div class="title-container">
            <h3 class="title">欢迎登录 实习管理系统</h3>
            <h4 class="subtitle">Internship Management System</h4>
          </div>
          <div class="form-content">
            <el-form-item :style='{"padding":"0","boxShadow":"0 0 6px rgba(0,0,0,0)","margin":"0 auto 20px","borderColor":"rgba(0,0,0,0)","backgroundColor":"rgba(0,0,0,0)","borderRadius":"0","borderWidth":"0","width":"85%","borderStyle":"solid","height":"auto"}' :class="'style2'">
              <span class="svg-container" style="
              color:#333;
              line-height:40px;
              font-size:16px;
              width:30px;
              padding:0;
              margin:0;
              radius:8px 0 0 8px;
              border-width:0;
              border-style:solid;
              border-color:rgba(0,0,0,0);
              background-color:rgba(255, 255, 255, 1);
              box-shadow:0 0 6px rgba(0,0,0,0);
              }"><svg-icon icon-class="user" /></span>
              <el-input placeholder="请输入用户名" name="username" type="text" v-model="rulesForm.username" />
            </el-form-item>
            <el-form-item :style='{"padding":"0","boxShadow":"0 0 6px rgba(0,0,0,0)","margin":"0 auto 20px","borderColor":"rgba(0,0,0,0)","backgroundColor":"rgba(0,0,0,0)","borderRadius":"0","borderWidth":"0","width":"85%","borderStyle":"solid","height":"auto"}' :class="'style2'">
              <span class="svg-container" style="color:#333;
              line-height:40px;
              font-size:16px;
              width:30px;
              padding:0;
              margin:0;
              radius:8px 0 0 8px;
              border-width:0;
              border-style:solid;
              border-color:rgba(0,0,0,0);
              background-color:rgba(255, 255, 255, 1);
              box-shadow:0 0 6px rgba(0,0,0,0);"><svg-icon icon-class="password" /></span>
              <el-input placeholder="请输入密码" name="password" type="password" v-model="rulesForm.password" />
            </el-form-item>
            <el-form-item :style='{"padding":"0","boxShadow":"0 0 6px rgba(0,0,0,0)","margin":"0 auto 20px","borderColor":"rgba(0,0,0,0)","backgroundColor":"rgba(0,0,0,0)","borderRadius":"0","borderWidth":"0","width":"85%","borderStyle":"solid","height":"auto"}' :class="'style2 code'">
              <span class="svg-container" style="color:#333;
              line-height:40px;
              font-size:16px;
              width:30px;
              padding:0;
              margin:0;
              radius:8px 0 0 8px;
              border-width:0;
              border-style:solid;
              border-color:rgba(0,0,0,0);
              background-color:rgba(255, 255, 255, 1);
              box-shadow:0 0 6px rgba(0,0,0,0);"><svg-icon icon-class="password" /></span>
              <el-input placeholder="请输入验证码(不区分大小写)" name="code" type="text" v-model="rulesForm.code" />
              <div class="getCodeBt" @click="getRandCode()">
                <span v-for="(item,index) in codes" :key="index" 
                  :style="{color:item.color,
                  transform:item.rotate,
                  fontSize:item.size,
                  fontWeight:'bold'}">
                  {{item.num}}
                </span>
              </div>
            </el-form-item>
            <el-form-item class="rememberMe" :style='{"padding":"0","boxShadow":"0 0 6px rgba(0,0,0,0)","margin":"0 auto 20px","borderColor":"rgba(0,0,0,0)","backgroundColor":"rgba(0,0,0,0)","borderRadius":"0","borderWidth":"0","width":"85%","borderStyle":"solid","height":"auto"}'>
              <el-checkbox v-model="rulesForm.rememberMe">记住密码</el-checkbox>
            </el-form-item>
            <el-form-item :style='{"padding":"0","boxShadow":"0 0 6px rgba(0,0,0,0)","margin":"0 auto 25px","borderColor":"rgba(0,0,0,0)","backgroundColor":"rgba(0,0,0,0)","borderRadius":"0","borderWidth":"0","width":"85%","borderStyle":"solid","height":"auto"}' class="role">
              <div class="role-label">请选择角色</div>
              <div class="role-buttons">
                <el-radio
                  v-for="item in menus"
                  v-if="item.hasBackLogin=='是'"
                  v-bind:key="item.roleName"
                  v-model="rulesForm.role"
                  :label="item.roleName"
                >{{item.roleName}}</el-radio>
              </div>
            </el-form-item>
            <el-button type="primary" @click="login()" class="loginInBt">登录</el-button>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>

import menu from "@/utils/menu";

export default {
  data() {
    return {
      rulesForm: {
        username: "",
        password: "",
        role: "",
        code: '',
        rememberMe: false
      },
      menus: [],
      tableName: "",
      codes: [{
        num: 1,
        color: '#000',
        rotate: '10deg',
        size: '16px'
      },{
        num: 2,
        color: '#000',
        rotate: '10deg',
        size: '16px'
      },{
        num: 3,
        color: '#000',
        rotate: '10deg',
        size: '16px'
      },{
        num: 4,
        color: '#000',
        rotate: '10deg',
        size: '16px'
      }],
    };
  },
  mounted() {
    let menus = menu.list();
    this.menus = menus;
  },
  created() {
    this.getRandCode()
  },
  methods: {
    register(tableName){
      this.$storage.set("loginTable", tableName);
      this.$router.push({path:'/register'})
    },
    // 登陆
    login() {
      if (!this.rulesForm.username) {
         this.$message.error("请输入用户名");
        return;
      }
      if (!this.rulesForm.password) {
         this.$message.error("请输入密码");
        return;
      }
      if (!this.rulesForm.role) {
         this.$message.error("请选择角色");
        return;
      }
      if (!this.rulesForm.code) {
         this.$message.error("请输入验证码");
        return;
      }
      
      // 验证验证码(不区分大小写)
      let checkCode = '';
      this.codes.forEach(item => {
        checkCode += item.num;
      });
      if (this.rulesForm.code.toLowerCase() !== checkCode.toLowerCase()) {
        this.$message.error("验证码错误");
        this.getRandCode();
        return;
      }

      let menus = this.menus;
      for (let i = 0; i < menus.length; i++) {
        if (menus[i].roleName == this.rulesForm.role) {
          this.tableName = menus[i].tableName;
        }
      }
      this.$http({
        url: `${this.tableName}/login?username=${this.rulesForm.username}&password=${this.rulesForm.password}`,
        method: "post"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$storage.set("Token", data.token);
          this.$storage.set("role", this.rulesForm.role);
          this.$storage.set("sessionTable", this.tableName);
          this.$storage.set("adminName", this.rulesForm.username);
          this.$router.replace({ path: "/index/" });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    getRandCode(len = 4){
      this.randomString(len)
    },
    randomString(len = 4) {
      let chars = [
          "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
          "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
          "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
          "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
          "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
          "3", "4", "5", "6", "7", "8", "9"
      ]
      let colors = ["0", "1", "2","3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]
      let sizes = ['14', '15', '16', '17', '18']

      let output = [];
      for (let i = 0; i < len; i++) {
        // 随机验证码
        let key = Math.floor(Math.random()*chars.length)
        this.codes[i].num = chars[key]
        // 随机验证码颜色
        let code = '#'
        for (let j = 0; j < 6; j++) {
          let key = Math.floor(Math.random()*colors.length)
          code += colors[key]
        }
        this.codes[i].color = code
        // 随机验证码方向
        let rotate = Math.floor(Math.random()*60)
        let plus = Math.floor(Math.random()*2)
        if(plus == 1) rotate = '-'+rotate
        this.codes[i].rotate = 'rotate('+rotate+'deg)'
        // 随机验证码字体大小
        let size = Math.floor(Math.random()*sizes.length)
        this.codes[i].size = sizes[size]+'px'
      }
    },
  }
};
</script>
<style lang="scss" scoped>
.loginIn {
  min-height: 100vh;
  position: relative;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
  background-color: #e6f3ff;
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320"><path fill="%234099ff" fill-opacity="0.2" d="M0,64L48,80C96,96,192,128,288,128C384,128,480,96,576,85.3C672,75,768,85,864,122.7C960,160,1056,224,1152,234.7C1248,245,1344,203,1392,181.3L1440,160L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z"></path></svg>');

  &:before {
    content: '';
    position: absolute;
    top: -100px;
    left: -100px;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    background-color: rgba(64, 158, 255, 0.15);
    z-index: -1;
  }

  &:after {
    content: '';
    position: absolute;
    bottom: -100px;
    right: -100px;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    background-color: rgba(64, 158, 255, 0.15);
    z-index: -1;
  }

  .loginInBt {
    width: 85%;
    height: 50px;
    line-height: 50px;
    margin: 10px auto 0;
    padding: 0;
    color: #fff;
    font-size: 18px;
    border-radius: 25px;
    border-width: 0;
    border-style: solid;
    border-color: #409EFF;
    background-color: #409EFF;
    box-shadow: 0 0 10px rgba(64,158,255,0.3);
    transition: all 0.3s ease;
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 0 15px rgba(64,158,255,0.5);
    }
  }

  .left {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-sizing: border-box;
    width: 450px;
    height: auto;
    margin: 0;
    padding: 40px 30px;
    border-radius: 20px;
    border-width: 0;
    border-style: solid;
    border-color: rgba(109, 109, 148, 1);
    background-color: rgba(255, 255, 255, 0.98);
    box-shadow: 0 0 30px rgba(64,158,255,0.2);

    .login-form {
      background-color: transparent;
      width: 100%;
      right: inherit;
      padding: 0;
      box-sizing: border-box;
      display: flex;
      position: initial;
      justify-content: center;
      flex-direction: column;
    }

    .form-content {
      width: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;
    }

    .title-container {
      text-align: center;
      margin-bottom: 30px;

      .title {
        width: 100%;
        line-height: 1.5;
        margin: 0 0 10px;
        padding: 0;
        color: #409EFF;
        font-size: 28px;
        font-weight: bold;
        border-radius: 0;
        border-width: 0;
        border-style: solid;
        border-color: transparent;
        background-color: transparent;
        box-shadow: none;
      }

      .subtitle {
        width: 100%;
        line-height: 1.5;
        margin: 0;
        padding: 0;
        color: #606266;
        font-size: 16px;
        font-weight: normal;
        border-radius: 0;
        border-width: 0;
        border-style: solid;
        border-color: transparent;
        background-color: transparent;
        box-shadow: none;
      }
    }

    .el-form-item {
      position: relative;
      margin-bottom: 20px;
      width: 100%;

      & /deep/ .el-form-item__content {
        line-height: initial;
      }

      .svg-container {
        padding: 6px 5px 6px 15px;
        color: #889aa4;
        vertical-align: middle;
        display: inline-block;
        position: absolute;
        left: 0;
        top: 0;
        z-index: 1;
        padding: 0;
        line-height: 45px;
        width: 30px;
        text-align: center;
      }

      .el-input {
        display: inline-block;
        width: 100%;

        & /deep/ input {
          background: #fff;
          border: 1px solid #dcdfe6;
          -webkit-appearance: none;
          padding: 0 15px 0 35px;
          color: #606266;
          height: 45px;
          width: 100%;
          line-height: 45px;
          margin: 0;
          border-radius: 22px;
          transition: all 0.3s ease;

          &:focus {
            border-color: #409EFF;
            box-shadow: 0 0 5px rgba(64,158,255,0.2);
          }
        }
      }
    }
    
    .rememberMe {
      margin-top: 5px;
      
      & /deep/ .el-checkbox {
        color: #666;
        font-size: 14px;
      }
    }
  }

  .center {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate3d(-50%,-50%,0);
  }

  .right {
    position: absolute;
    left: inherit;
    right: 0;
    top: 0;
  }

  .code {
    .el-form-item__content {
      position: relative;

      .getCodeBt {
        position: absolute;
        right: 5px;
        top: 50%;
        transform: translate3d(0, -50%, 0);
        line-height: 35px;
        width: 110px;
        background-color: #f5f7fa;
        color: #606266;
        text-align: center;
        border-radius: 18px;
        height: 35px;
        overflow: hidden;
        padding: 0;
        margin: 0;
        border-width: 0;
        border-style: solid;
        border-color: rgba(64, 158, 255, 1);
        background-color: #f5f7fa;
        box-shadow: 0 0 6px rgba(0,0,0,0.1);
        cursor: pointer;
        transition: all 0.3s ease;

        &:hover {
          background-color: #ecf5ff;
          box-shadow: 0 0 8px rgba(64,158,255,0.2);
        }

        span {
          padding: 0 4px;
          display: inline-block;
          font-size: 16px;
          font-weight: 600;
        }
      }

      .el-input {
        & /deep/ input {
          padding: 0 130px 0 35px;
        }
      }
    }
  }

  .setting {
    & /deep/ .el-form-item__content {
      box-sizing: border-box;
      line-height: 32px;
      height: 32px;
      font-size: 14px;
      color: #999;
      margin: 20px 0 0 !important;
      display: flex;
      justify-content: center;

      .account-login {
        text-align: center;
        color: #409EFF;
        cursor: pointer;
      }
    }
  }

  .style2 {
    padding-left: 30px;

    .svg-container {
      left: 5px !important;
    }

    .el-input {
      & /deep/ input {
        padding-left: 35px !important;
      }
    }
  }

  .code.style2 {
    .el-input {
      & /deep/ input {
        padding: 0 130px 0 35px !important;
      }
    }
  }

  .style3 {
    & /deep/ .el-form-item__label {
      padding-right: 6px;
      height: 40px;
      line-height: 40px;
    }

    .el-input {
      & /deep/ input {
        padding: 0 15px !important;
      }
    }
  }
  
  & /deep/ .el-form-item__label {
    width: 30px;
    height: 40px;
    line-height: 40px;
    margin: 0;
    padding: 0;
    color: #333;
    font-size: 14px;
    border-radius: 8px 0 0 8px;
    border-width: 0;
    border-style: solid;
    border-color: rgba(0,0,0,0);
    background-color: rgba(255, 255, 255, 1);
    box-shadow: 0 0 6px rgba(0,0,0,0);
  }

  .role {
    margin: 20px 0 !important;
    text-align: center;
    width: 100%;

    .role-label {
      color: #409EFF;
      font-size: 16px;
      font-weight: bold;
      margin-bottom: 15px;
      text-align: center;
    }

    .role-buttons {
      display: flex;
      justify-content: center;
      flex-wrap: wrap;
      gap: 10px;
    }

    & /deep/ .el-radio {
      margin: 0;
      min-width: 100px;
      padding: 8px 15px;
      border-radius: 20px;
      transition: all 0.3s ease;
      background-color: #fff;
      border: 1px solid #dcdfe6;
      cursor: pointer;
      text-align: center;

      &:hover {
        background-color: #f5f7fa;
        border-color: #409EFF;
      }

      &.is-checked {
        background-color: #ecf5ff;
        border-color: #409EFF;
        color: #409EFF;
      }

      & /deep/ .el-radio__label {
        font-size: 14px;
        color: #606266;
        padding: 0;
      }

      &.is-checked /deep/ .el-radio__label {
        color: #409EFF;
      }

      & /deep/ .el-radio__inner {
        display: none;
      }
    }
  }
}
</style>
